import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/common/prisma/PrismaService';
import ExamExamineeQueryParam from '../param/ExamExamineeQueryParam';
import { plainToInstance } from 'class-transformer';
import ExamExamineeVo from '../vo/ExamExamineeVo';
import ExamExamineeDto from '../dto/ExamExamineeDto';
import ServiceException from 'src/common/ServiceException';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-16 14:48:48
 * @LastEditTime: 2024-12-16 16:01:36
 * @Description: 考生service
 */
@Injectable()
class ExamExamineeService {
  constructor(private prisma: PrismaService) {}

  async list(param: ExamExamineeQueryParam) {
    const skip = param.pageNum != 0 ? (param.pageNum - 1) * param.pageSize : 0;
    const whereClause: any = this.generatePageQueryCondition(param);
    const [total, list] = await this.prisma.$transaction([
      this.prisma.examExaminee.count({ where: whereClause }),
      this.prisma.examExaminee.findMany({
        skip,
        take: param.pageSize,
        orderBy: {
          createTime: 'desc',
        },
        where: whereClause,
      }),
    ]);
    const rows: ExamExamineeVo[] = plainToInstance(ExamExamineeVo, list);
    return {
      total,
      rows,
    };
  }
  generatePageQueryCondition(param: ExamExamineeQueryParam) {
    const whereClause: any = {};
    if (param.name) {
      whereClause.name = {
        contains: param.name,
      };
    }
    if (param.phone) {
      whereClause.phone = {
        contains: param.phone,
      };
    }
    if (param.cardNo) {
      whereClause.cardNo = {
        contains: param.cardNo,
      };
    }
    if (param.sex) {
      whereClause.sex = param.sex;
    }
    if (param.admissionTicket) {
      whereClause.admissionTicket = {
        contains: param.admissionTicket,
      };
    }
    return whereClause;
  }

  async detail(id: number) {
    const examinee = await this.prisma.examExaminee.findUnique({
      where: { id },
    });
    return plainToInstance(ExamExamineeVo, examinee);
  }

  async edit(data: ExamExamineeDto) {
    await this.checkExamExamineeCard(data.cardNo, data.id);
    return await this.prisma.examExaminee.update({
      where: { id: data.id },
      data: {
        name: data.name,
        sex: data.sex,
        phone: data.phone,
        cardNo: data.cardNo,
        admissionTicket: data.admissionTicket,
      },
    });
  }
  async checkExamExamineeCard(cardNo: string, id: number) {
    const examinee = await this.prisma.examExaminee.findFirst({
      where: { cardNo: cardNo },
    });
    if (examinee && examinee.id != id) {
      throw new ServiceException('考生身份证号：' + cardNo + '已存在');
    }
  }

  async add(data: ExamExamineeDto) {
    await this.checkExamExamineeCard(data.cardNo, data.id);
    return await this.prisma.examExaminee.create({
      data: {
        name: data.name,
        sex: data.sex,
        phone: data.phone,
        cardNo: data.cardNo,
        admissionTicket: data.admissionTicket,
      },
    });
  }

  async remove(id: number) {
    await this.prisma.examExaminee.delete({ where: { id } });
  }
}
export default ExamExamineeService;
